﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using VersionManage.Logic;
namespace VersionManage
{
    public partial class FormAddVersion : Form
    {
        private ExeVersion ev;
        private string selectpath;
        public FormAddVersion()
        {
            InitializeComponent();
        }
        public FormAddVersion(ExeVersion _ev)
        {
            InitializeComponent();
            this.ev = _ev;
            this.Load += FormAddVersion_Load;
        }

        private void FormAddVersion_Load(object sender, EventArgs e)
        {
            if (ev == null)
                return;
            textBoxName.Text = ev.Name;
            textBoxVersion.Text = ev.Num;
            textBoxAppCode.Text = ev.AppCode;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                selectpath = folderBrowserDialog1.SelectedPath;
                textBoxPath.Text = selectpath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(selectpath))
                return;
            new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                Logic.ExeVersion version = new Logic.ExeVersion();
                version.Name = textBoxName.Text;
                version.Num = textBoxVersion.Text;
                version.AppCode = textBoxAppCode.Text;


                //ossh上传
                List<Logic.RemoteFile> Files = UploadToOss(selectpath);


                //服务器上传
                //List<Logic.RemoteFile> Files = UploadToServer(selectpath);


                Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
                version.FilesJson = Newtonsoft.Json.JsonConvert.SerializeObject(Files);

                HttpItem http = new HttpItem { Method = HttpMethod.Post, Url = ServicesAddress.VersionManageAddress, ContentType = "application/json" };
                http.Content = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(version));
                HttpResult<string> result = WebLogic.GetHttpResult<string>(http);
                this.DialogResult = DialogResult.OK;
            })).Start();
        }


        /// <summary>
        /// 服务器文件上传
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private List<string> GetFiles(string path)
        {
            List<string> remoteFiles = new List<string>();
        
            string[] files = Directory.GetFiles(path);
            if (files != null && files.Length > 0)
                remoteFiles.AddRange(files);
        
            string[] dires = Directory.GetDirectories(path);
            if (dires != null && dires.Length > 0)
            {
                dires.ToList().ForEach(o =>
                {
                    List<string> fs = GetFiles(o);
                    if (fs != null)
                        remoteFiles.AddRange(fs);
                });
            }
            return remoteFiles.Count > 0 ? remoteFiles : null;
        }


        /// <summary>
        /// OSS 文件上传
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private List<Logic.RemoteFile> GetFilesoss(string path)
        {
            List<Logic.RemoteFile> remoteFiles = new List<Logic.RemoteFile>();

            string[] files = Directory.GetFiles(path);
            if (files != null && files.Length > 0)
                remoteFiles.AddRange(files.Select(o => new Logic.RemoteFile { name = o }));

            string[] dires = Directory.GetDirectories(path);
            if (dires != null && dires.Length > 0)
            {
                dires.ToList().ForEach(o =>
                {
                    List<RemoteFile> fs = GetFilesoss(o);
                    if (fs != null)
                        remoteFiles.AddRange(fs);
                });
            }
            return remoteFiles.Count > 0 ? remoteFiles : null;
        }




        private List<Logic.RemoteFile> UploadToOss(string path)
        {
            List<RemoteFile> Files = GetFilesoss(selectpath);
            this.Invoke(new Action(() => progressBar1.Maximum = Files.Count));
            int count = selectpath.Length + 1;

            foreach (var item in Files)
            {
                Logic.AliObjectStorage aliObject = Logic.FileLogic.GetFileMD5(item.name);
                if (Logic.AliOSS.CreateAliOSS().Put(aliObject))
                {
                    item.name = item.name.Remove(0, count);
                    item.md5 = aliObject.FileName;
                    item.url = aliObject.OSSPath;
                    this.Invoke(new Action(() => progressBar1.Value += 1));
                }
            }
            return Files;
        }

        private List<Logic.RemoteFile> UploadToServer(string path)
        {
            List<RemoteFile> Files = new List<RemoteFile>();
            List<string> FileInfos = GetFiles(selectpath);
            this.Invoke(new Action(() =>
            {
                progressBar1.Minimum = 0;
                progressBar1.Maximum = FileInfos.Count;
                progressBar1.Value = 0;
            }));
            int count = selectpath.Length + 1;

            foreach (var item in FileInfos)
            {
                RemoteFile rf = FileLogic.UploadFile(item);
                this.Invoke(new Action(() => progressBar1.Value++));
                rf.name = item.Remove(0,count);
                Files.Add(rf);
            }
            return Files;
        }
    }
}
